package com.jwgf.itemviewdelegates;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.jwgf.protocolstack.CommunicationStateListener;
import com.jwgf.protocolstack.ProtocolStack;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.ui.SmaroWindingMachineActivity;

import org.greenrobot.eventbus.EventBus;

public class EditAttr implements CommunicationStateListener.WriteFailedListener, CommunicationStateListener.WriteSucceedListener {
    private MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    private int group;
    private int index;
    private int subIndex;
    private int position;
    private TextView.OnEditorActionListener listener;

    private String name;
    private String value;
    private String unit;


    public int getGroup() {
        return group;
    }

    public void setGroup(int group) {
        this.group = group;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getSubIndex() {
        return subIndex;
    }

    public void setSubIndex(int subIndex) {
        this.subIndex = subIndex;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public TextView.OnEditorActionListener getListener() {
        if(listener == null) {
            listener = new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        SmaroWindingMachineActivity.canHandStack.registerWriteFailedListener(EditAttr.this);
                        SmaroWindingMachineActivity.canHandStack.registerWriteSucceedListener(EditAttr.this);
                        String strValue = v.getText().toString();
                        if (TextUtils.isEmpty(strValue)) {
                            return false;
                        }
                        if (Integer.valueOf(strValue) < 65536) {
                            SmaroWindingMachineActivity.canHandStack.putPriorityCmd(sp.getBroadcast());
                            SmaroWindingMachineActivity.canHandStack
                                    .putPriorityCmd(sp.createCanCmd(getGroup(), getIndex(), getSubIndex(), strValue));
                            SmaroWindingMachineActivity.canHandStack.putPriorityCmd(sp.releaseBroadcast());
                        }
                    }
                    return false;
                }
            };
        }
        return listener;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    @Override
    public void writeFailed(ProtocolStack ps, byte[] request) {
        EventBus.getDefault().post(this);
    }

    @Override
    public void writeSucceed(ProtocolStack ps, byte[] request) {
        short index = (short) (request[2] & 0xff);
        if ((index >= 0x41 && index <= 0x4f) || (index >= 0x70 && index <= 0x9f) || (index >= 0xB0 && index <= 0xB4)) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
            sp.setValue(getGroup(), index, request);
            setValue(sp.getValue(getGroup(), index, subIndex));
            EventBus.getDefault().post(this);

        }
    }
}
